# -*- encoding: utf-8 -*-
"""
File Name: dedicated_line_monitor
Description:
Author : 'lirui'
date: 2024/1/26
Contact: marjey_lee@163.com
Change Activity:
2024/1/26:
"""
import copy

import netmiko

import ll
from ll.utils.en_de import de_s
from projects.monitoring.branch_dedicated_line_monitor.config.config import CHONGQING_LOGIN_INFO
from projects.monitoring.branch_dedicated_line_monitor.config.ospf import CONGQING_IP_LIST


class LoadChongQingBranchLineStatus:
    def __init__(self):
        super().__init__()
        self.results = {}
        self.normal_ips = []
        self.error_ips = []
        self.running_success = True

    @staticmethod
    def _get_router_running_config():
        res_lst = ''
        info = copy.deepcopy(CHONGQING_LOGIN_INFO)
        for router_name, login_info in info.items():
            login_info['username'] = de_s(login_info['username'], 'networknetworknetwork')
            login_info['password'] = de_s(login_info['password'], 'networknetworknetwork')
            net_connect = netmiko.ConnectHandler(**login_info)
            # 去掉长度限制
            net_connect.send_command('screen-length 0 temporary')
            net_connect.find_prompt()
            res = net_connect.find_prompt() + 'display ospf peer brief' + '\n'
            res += net_connect.send_command_timing('display ospf peer brief')
            res_ = res
            if '^' in res:
                if 'invalid' in res.lower() or 'position' in res.lower():
                    res_ = login_info['host'] + '_' + '显示OSPF' + ' ———— 指令执行失败'
            res_lst = res_lst + res_
            net_connect.disconnect()
        return res_lst

    def __call__(self):
        """
        monitor dedicated line
        """
        ll.get_logger().info('Start monitor chongqing branch dedicated line.')
        router_config = self._get_router_running_config()
        self._classify(router_config)
        pass

    def _classify(self, router_config):
        """

        Returns:

        """
        for ip in CONGQING_IP_LIST:
            if ip not in router_config:
                self.error_ips.append(ip)
                self.results[ip] = 0
            else:
                self.normal_ips.append(ip)
                self.results[ip] = 1


def _main():
    monitor = LoadChongQingBranchLineStatus()
    monitor()


if __name__ == '__main__':
    _main()
